package com.forusers.escrevecheque.client;

/**
 * A very simple difference routine that puts bold where the differences were found.
 * @author scott
 *
 */
public class HighlightDiff {
	String[] correctWords;
	String[] usersWords;
	
	HighlightDiff(String correct, String users) {
		correctWords = correct.split(" ");
		usersWords = users.split(" ");
	}
	
	String getMarkedUpAnswer() {
		StringBuffer ret = new StringBuffer();
		for (int i = 0; i < correctWords.length; ++i) {
			boolean match = false;
			final String curWord = correctWords[i];
			for (int j = -1; j < 2; j++) {
				int ij = i + j;
				if (ij >= 0 && ij < usersWords.length 
						&& curWord.equals(usersWords[ij])) {
					match = true;
					break;
				}
			}
			if (!match) {
				ret.append("<b>");
			}
			ret.append(curWord);
			if (!match) {
				ret.append("</b> ");
			} else {
				ret.append(" ");
			}
		}
		return ret.toString();
	}
}
